package graphics.server;

import java.applet.Applet;
import java.awt.Color;
import java.awt.GridLayout;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Isaac
 *
 * This class take care of the visual.
 */

public class Boom extends Applet
{
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	public Icon iconAN,iconAW,iconAE,iconANRed,iconAWRed,iconAERed,icon6,
		   icon7,iconv,iconb,iconflag,iconice,iconb_2,iconboomEror;
	public String pict[] ={"images/AgentNorth.jpg","images/AgentSouth.jpg","images/AgentWest.jpg","images/AgentEast.jpg","images/AgentNorthRed.jpg",
			"images/AgentSouthRed.jpg","images/AgentWestRed.jpg","images/AgentEastRed.jpg","images/view.jpg","images/boom.jpg","images/flag.jpg","images/ice.jpg",
			"images/GhostNorth.jpg","images/GhostWest.jpg","images/GhostSouth.jpg","images/GhostEast.jpg"};
	public JPanel p;
	public JLabel[] b;

//************************************************************************************

/**
 * Create a board and initialize it.
 */
public void init(int Xsize,int Ysize)
{
	iconv = getImage(pict[8]);
	iconb = getImage(pict[9]);
	iconflag = getImage(pict[10]);
	iconice = getImage(pict[11]);

	p = new JPanel();
	p.setLayout(new GridLayout(Ysize,Xsize));
	b = new JLabel[Ysize*Xsize];
	for(int j=0;j<(Ysize*Xsize);j++)
	{
		b[j] = new JLabel(iconv);
		b[j].setBorder(BorderFactory.createLineBorder(Color.black));
		p.add(b[j]);
	}

	for(int j=0;j<Xsize;j++)
		b[j].setIcon(iconb);

	for(int j=Xsize;j<((Ysize*Xsize)-Xsize);j+=Xsize){
		b[j].setIcon(iconb);
		b[j+Xsize-1].setIcon(iconb);
	}
	for(int j=((Ysize*Xsize)-Xsize);j<(Ysize*Xsize);j++){
		b[j].setIcon(iconb);
	}
	setLayout(new GridLayout());
	add(p);
}
//***********************************************
/* Sets a Wall */
public void setWall(int cell){
	b[cell].setIcon(iconb);
}
//*************************************************
/* Sets Flags */
public void setFlag(int cell){
	b[cell].setIcon(iconflag);
}
/* Sets Ice */
public void setIce(int cell){
	b[cell].setIcon(iconice);
}
/* delete the content of a cell */
public void deleteContent(int cell){
	b[cell].setIcon(iconv);
}


//********************************************************************
	/* Initialize the agent location  */
	public void setAgent(SingleAgent agent) {
		int offset =findIconOffset((String)(agent.getColor()),(String)(agent.getOrientation()));
		b[agent.getLocation()].setIcon(getImage(pict[offset]));
	}

	private int findIconOffset (String color,String orientation){
		int offset = 0;
		if(color.compareToIgnoreCase("Red") == 0)
			offset = 4;
		if (orientation.compareToIgnoreCase("South")==0)
			offset += 1;
		else if (orientation.compareToIgnoreCase("West")==0)
			offset += 2;
		else if (orientation.compareToIgnoreCase("East")==0)
			offset += 3;
		return offset;
	}
	
	private ImageIcon getImage(String path){
		URL url = getClass().getResource("/" + path);
		if (url == null){
			return new ImageIcon(path);
		} else {
			try {
				return new ImageIcon(ImageIO.read(url));
			} catch (IOException e) {
				return new ImageIcon(path);
			}
		}
	}
}



